package com.wbao.dianniu.utils;

import android.content.Context;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.logical.GlobalContext;

import java.util.ArrayList;

/**
 * 网络图片处理工具类
 *
 * @author yejianjun
 */
public class PictureDownloadUtils {
    //namespace
    public static final String namespace = "dianniu";
    private static PictureDownloadUtils instance;
    String mTaskId;

    public static PictureDownloadUtils getInstance() {
        if (null == instance) {
            instance = new PictureDownloadUtils();
        }
        return instance;
    }

    /**
     * String 图片数组转成list
     *
     * @param str
     * @return
     */
    public ArrayList<String> stringToList(String str) {
        ArrayList<String> list = new ArrayList<String>();
        String[] result = str.split(",");
        for (int i = 0; i < result.length; i++) {
            list.add(GlobalContext.cdndownUrl + result[i]);
        }
        return list;
    }

    /**
     * 自身头像--大图查看 支持缩放
     *
     * @return
     */
    /*public static DisplayImageOptions  getHeadOptions(){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.default_head_icon) // resource or drawable  
        .showImageForEmptyUri(R.drawable.default_head_icon) // resource or drawable  
        .showImageOnFail(R.drawable.default_head_icon) // resource or drawable  
        .delayBeforeLoading(1000)  
        .cacheInMemory(true) // default  
        .cacheOnDisk(false) // default  
        .imageScaleType(ImageScaleType.NONE) // default  
        .bitmapConfig(Bitmap.Config.RGB_565) // default  
        .delayBeforeLoading(100)//载入图片前稍做延时可以提高整体滑动的流畅度
        .build();
		return options;
	}*/

    /**
     * 加载头像并显示  图片地址没带前缀
     *
     * @param context
     * @param view
     * @param headPic
     */
    public static void displayHeadImage(Context context, ImageView view, String headPic) {
        String downUrl = GlobalContext.cdndownUrl + headPic; //+ "@100-1ci.png";
        if(null != context){
            Glide.with(context)
                    .load(downUrl)
                    .centerCrop()
                    .placeholder(R.drawable.default_head_icon)
                    .crossFade()
                    .override(Utils.dip2px(context, 40), Utils.dip2px(context, 40))
                    .bitmapTransform(new GlideCircleTransform(context))
                    .thumbnail(0.2f)
                    .into(view);
        }
    }

    /**
     * 显示默认头像
     *
     * @param context
     * @param imageView
     */
    @Deprecated
    public static void displayDefaultHeadImg(Context context, ImageView imageView) {
        if(null != context){
            Glide.with(context)
                    .load(R.drawable.default_head_icon)
                    .centerCrop()
                    .placeholder(R.drawable.default_head_icon)
                    .crossFade()
                    .override(Utils.dip2px(context, 40), Utils.dip2px(context, 40))
                    .into(imageView);
        }
    }


    /**
     * 加载头像并显示 图片地址带了前缀
     *
     * @param context
     * @param view
     * @param headPic
     */
    public static void displayHeadNotPrefix(Context context, ImageView view, String headPic) {
        if(null != context){
            Glide.with(context)
                    .load(headPic)
                    .centerCrop()
                    .placeholder(R.drawable.default_head_icon)
                    .crossFade()
                    .override(Utils.dip2px(context, 40), Utils.dip2px(context, 40))
                    .thumbnail(0.2f)
                    .into(view);
        }
    }

    public static void loadingAdImage(Context context, ImageButton imageButton, String imageUrl) {
        if(null != context){
            Glide.with(context)
                    .load(imageUrl)
                    .into(imageButton);
        }
    }


}
